package com.my.assembly.handler;

import android.app.Activity;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.laojiang.imagepickers.ImagePicker;
import com.laojiang.imagepickers.data.ImagePickType;
import com.laojiang.imagepickers.data.MediaDataBean;
import com.laojiang.imagepickers.utils.GlideImagePickerDisplayer;
import com.my.assembly.bridge.BridgeHandler;
import com.my.assembly.bridge.BridgeWebView;
import com.my.assembly.bridge.CallBackFunction;
import com.my.assembly.handler.utils.UploadCallback;
import com.my.assembly.handler.utils.UploadFileUtil;
import com.my.assembly.utils.HandlerUtils;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;

/**
 * <pre>
 *     author : wxq
 *     e-mail :
 *     time   : 2018/11/27
 *     desc   : 打开相册组件
 *     version: 1.0
 * </pre>
 */
public class OpenVideoHandler implements BridgeHandler {

    public static final int REQUEST_CODE = 0x0001;
    public static final int RESULT_CODE = 0x0002;

    private Activity mContext;

    private CallBackFunction mFunction;
    private BridgeWebView mWebView;

    private UploadFileUtil uploadFileUtil;

    public boolean isUpload = false;

    public OpenVideoHandler(Activity context, BridgeWebView webView) {
        this.mContext = context;
        this.mWebView = webView;
        uploadFileUtil = new UploadFileUtil();
    }

    @Override
    public void handler(String data, CallBackFunction function) {
        this.mFunction = function;
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(data);
        int maxNum = 1;
        ImagePicker build = new ImagePicker.Builder()
                .pickType(ImagePickType.MUTIL) //设置选取类型(拍照ONLY_CAMERA、单选SINGLE、多选MUTIL)
                .maxNum(maxNum) //设置最大选择数量(此选项只对多选生效，拍照和单选都是1，修改后也无效)
                .needCamera(false) //是否需要在界面中显示相机入口(类似微信那样)
                .cachePath(mContext.getCacheDir().getPath()) //自定义缓存路径(拍照和裁剪都需要用到缓存)
                .doCrop(1, 1, 300, 300) //裁剪功能需要调用这个方法，多选模式下无效，参数：aspectX,aspectY,outputX,outputY
                .displayer(new GlideImagePickerDisplayer()) //自定义图片加载器，默认是Glide实现的,可自定义图片加载器
                .needVideo(true)//是否显示视频
                .build();

        build.start(mContext, REQUEST_CODE, RESULT_CODE); //自定义RequestCode和ResultCode
        isUpload = true;
    }

    public void uploadFile(ArrayList<MediaDataBean> fileData) {
        Hawk.put("selectImg", tran(Hawk.get("selectImg", new ArrayList<String>()), fileData));
        isUpload = false;
        uploadFileUtil.upload(fileData, new UploadCallback() {
            @Override
            public void start() {
            }

            @Override
            public void uploadProgress(int progress) {
            }

            @Override
            public void success(final String result) {
//                mFunction.onCallBack(result);
                HandlerUtils.post(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.callHandler("videoupload", result, new CallBackFunction() {
                            @Override
                            public void onCallBack(String data) {

                            }
                        });
                    }
                });
            }

            @Override
            public void fail(String message) {
//                mFunction.onCallBack(message);
            }
        });
    }

    private ArrayList<String> tran(ArrayList<String> sourceList, ArrayList<MediaDataBean> fileData) {
        for (MediaDataBean bean : fileData) {
            sourceList.add(bean.getMediaPath());
        }
        return sourceList;
    }
}
